//波次单
$(function(){
	//搜索
	$('.searchBar').click(function(){
		var thisform=$(this).parent('form');
		var logistics_no=thisform.find('input[name=logistics_no]').val();
		var company_name=thisform.find('input[name=company_name]').val();
		var goods_name=thisform.find('input[name=goods_name]').val();
		var goods_no=thisform.find('input[name=goods_no]').val();
		var check_cate=thisform.find('input[name=check_cate]').val();
		var logiscompany_name=thisform.find('input[name=logiscompany_name]').val();
		var nums=thisform.find('input[name=nums]').val();
		if(goods_name!="" || goods_no!=""){
			if(check_cate==0){
				$.messager.alert('错误提示','请选择匹配规则','error');
				return false;
			}
			if(check_cate==1 || check_cate==2){
				if(nums=='' || nums==0){
					alert('数量不能为空或0');
					return false;
				}
				// if(goods_name==''){
					// alert('品名不能为空');
				// }
			}
		}
		if(check_cate==2 || check_cate==3){
			if(goods_name==""){
				$.messager.alert('错误提示','商品名称不能为空','error');
				return false;
			}
		}
		$('#pushorderDatagrid').datagrid('options').pageNumber=1;//设置页码初始值为1
		$('#pushorderDatagrid').datagrid({
			queryParams:{
				nums:nums,
				company_name:company_name,
				goods_name:goods_name,
				goods_no:goods_no,
				check_cate:check_cate,
				logiscompany_name:logiscompany_name,
				logistics_no:logistics_no
			}
		});
	})
	$('#pushorderDatagrid').datagrid({
		//url:getOrderUrl+"?randnum="+Math.floor(Math.random()*1000000),
		url:getOrderUrl,
		title:'波次单',
		fitColumns:true,
		rownumbers:true,
		pageSize:32,
		pageNumber:1,
		pageList:[5,10,15,20,25,30,32,40,50],
		columns:[[
		{field:'id',title:'id',checkbox:true},
		{field:'order_no',title:'订单编码',sortable:true},
		{field:'logistics_no',title:'运单号',sortable:true},
		{field:'user_name',title:'收件人姓名'},
		{field:'user_phone',title:'联系电话'},
		{field:'user_idcard',title:'身份证'},
		{field:'province',title:'省',sortable:true},
		{field:'city',title:'市',sortable:true},
		{field:'area',title:'区',sortable:true},
		{field:'user_address',title:'客户详细地址',width:'100px',
				formatter:function(val){
					return "<span title='"+val+"' alt='"+val+"' style='cursor:pointer;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;width:100px;float:left;'>"+val+"</span>";
				}
		},
		{field:'platform_name',title:'订单来源'},
		{field:'logiscompany_name',title:'快递公司',sortable:true},
		{field:'company_name',title:'商户名称'},
		{field:'add_time',title:'接收时间',sortable:true},
		{field:'erp_mark_mailno',title:'erp备注'},
		{field:'arrow_status',title:'置顶',
				formatter:function(val,rowData,rowIndex){
					if(val==1){
						return '是';
					}
					
			    }
		},
		{field:'a.times',title:'商品编码',
				formatter:function(val,rowData,rowIndex){
			       // console.log(rowData);
					if(rowData.goods_list==null){
						return '没有商品详情';
					}
					var header="<table style='width:100%;'>";
					var footer="</table>";
					var body="";
					for(i=0;i<rowData.goods_list.length;i++){
						body=body+"<tr><td style='border:0px;'>"+rowData.goods_list[i].goods_no+"</td></tr>";
					}
					var text=header+body+footer;
					return text;
			    }
		},
		{field:'b.times',title:'商品名称',width:'210px',
				formatter:function(val,rowData,rowIndex){
			       // console.log(rowData);
					if(rowData.goods_list==null){
						return '没有商品详情';
					}
					var header="<table style='width:100%;'>";
					var footer="</table>";
					var body="";
					for(i=0;i<rowData.goods_list.length;i++){
						body=body+"<tr><td title='"+rowData.goods_list[i].goods_name+"' alt='"+rowData.goods_list[i].goods_name+"' style='border:0px;cursor:pointer;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;width:200px;float:left;'>"+rowData.goods_list[i].goods_name+"</td></tr>";
					}
					// body=body+"<tr height=10 border=0><td colspan='3'></td></tr>";
					var text=header+body+footer;
					return text;
			    }
		},
		{field:'c.times',title:'数量',
				formatter:function(val,rowData,rowIndex){
			       // console.log(rowData);
					if(rowData.goods_list==null){
						return '没有商品详情';
					}
					var header="<table style='width:100%;'>";
					var footer="</table>";
					var body="";
					for(i=0;i<rowData.goods_list.length;i++){
						body=body+"<tr><td style='border:0px;'>"+rowData.goods_list[i].goods_nums+"</td></tr>";
					}
					// body=body+"<tr height=10 border=0><td colspan='3'></td></tr>";
					var text=header+body+footer;
					return text;
			    }
		}
		]],
		loadMsg:'正在加载订单，请稍等...',
		onLoadError:function(data){
			var info=eval('('+data.responseText+')');
			$.messager.confirm('错误提示',info.message,function(r){
				var tab=$('#Tabs').tabs('getSelected');
				var index=$('#Tabs').tabs('getTabIndex',tab);
				$('#Tabs').tabs('close',index);
			});
		},
		onBeforeLoad:function(data){
			data.is_emergency=is_emergency;
        },
		loadFilter: function(data){
			// console.log(data.status);
			if(data.status==0){
				$.messager.alert('错误提示',data.message,'error');
				return false;
			}else{
				return data;
			}
		},
		rowStyler:function(index,row){
			if (row.color==0){
				return 'background-color:pink;color:blue;font-weight:bold;';
			}
			if (row.arrow_status==1){
				return 'background-color:#fff3f3;color:#3F51B5;font-weight:bold;';
			}
			if (row.arrow_status==-1){
				return 'background-color:#009688;color:#fff;font-weight:bold;';
			}
		},
		pagination:true,
		fit:true,
	});
	$('#pickcollectDatagrid').datagrid({
		//url:getCollectgoodsUrl+"?randnum="+Math.floor(Math.random()*1000000),
		url:getCollectgoodsUrl,
		fitColumns:true,
		rownumbers:true,
		columns:[[
		{field:'goods_no',title:'商品编码',sortable:true},
		{field:'goods_name',title:'商品名称'},
		{field:'goods_count',title:'数量',sortable:true},
		{field:'storage_code',title:'库位',sortable:true},
		{field:'storage_level',title:'库位层级',sortable:true}
		]],
		onLoadError:function(data){
			var info=eval('('+data.responseText+')');
			$.messager.confirm('错误提示',info.message,function(r){
				var tab=$('#Tabs').tabs('getSelected');
				var index=$('#Tabs').tabs('getTabIndex',tab);
				$('#Tabs').tabs('close',index);
			});
		},
		rowStyler:function(index,row){
			if (row.color==0){
				return 'background-color:pink;color:blue;font-weight:bold;';
			}
		}	
	});
});
pushorderObj={
	make:function(){//确定生成波次单
		var orderlist=$('#pickcollectMakeButtons').attr('attr_orderlist');
		// alert(orderlist);
		$.post(makePicklistUrl,{orderlist:orderlist},function(result){
			
			if (!result.status){
				$.messager.alert('错误提示',result.message,'error');
			}else{
				// console.log(result);
				$('#pickcollectAddDialog').dialog('open').dialog('setTitle','生成波次单');
				$('#pickcollectDatagrid').datagrid({
					title:"订单编号:"+orderlist
				});
				$.messager.alert('确定',result.message,'info',function(r){
					$('#pickcollectAddDialog').dialog('close');
					window.parent.showTab(picklistViewUrl,'波次单详情',result.picklist_id);
				});
			}
		},'json').error(function(data){
			var info=eval('('+data.responseText+')');
			$.messager.confirm('错误提示',info.message,function(r){
				//$('#authAccessSetDialog').dialog('close');
			});
		});
	},
	arrow_status:function(value){
		var Row=$('#pushorderDatagrid').datagrid('getSelections');
		if(Row.length<1){
			$.messager.alert('提示','请选择列表数据','error');
		}else{
			var ids=[];
            for(var i=0;i<Row.length;i++){
            	ids.push(Row[i].id);
            }
            var id=ids.join(',');
        	$.post(arrow_statusUrl,{id:id,value:value},function(result){
				if (!result.status){
					$.messager.alert('错误提示',result.message,'error');
				}else{
					$('#pushorderDatagrid').datagrid({ queryParams:''});
				}
			},'json').error(function(data){
				var info=eval('('+data.responseText+')');
				$.messager.confirm('错误提示',info.message,function(r){
					//$('#authAccessSetDialog').dialog('close');
				});
			});
        }
	},
	makeBar:function(){
		var pushorderRow=$('#pushorderDatagrid').datagrid('getSelections');
        if (pushorderRow.length>make_max){		            	
            $.messager.alert('提示','一次只能操作'+make_max+'条记录!','info');
        }else if(pushorderRow.length<1){
			$.messager.alert('推送提示','请选择列表数据','error');
		}else{
			var ids=[];
            for(var i=0;i<pushorderRow.length;i++){
            	ids.push(pushorderRow[i].id);
            }
            var id=ids.join(',');
        	$.post(makePickcollectUrl,{id:id},function(result){
				if (!result.status){
					$.messager.alert('错误提示',result.message,'error');
				}else{
					orderlist=result.orderlist;
					
					$('#pickcollectAddDialog').dialog('open').dialog('setTitle','生成波次单');
					$('#pickcollectDatagrid').datagrid({
						title:"订单编号:"+orderlist
					});
					$('#pickcollectMakeButtons').attr('attr_orderlist',orderlist);
				}
			},'json').error(function(data){
				var info=eval('('+data.responseText+')');
				$.messager.confirm('错误提示',info.message,function(r){
					//$('#authAccessSetDialog').dialog('close');
				});
			});
        }
	},
	showAll:function(){
		$('#pushorderDatagrid').datagrid({ queryParams:''});
	},
	showAdvanceBar:function(){
		$('#pushorderDatagrid').datagrid({ queryParams:{show_advance:1}});
	},
	historyPicklistBar:function(){
		window.parent.showTab(historyPicklistUrl,'历史波次单');
	}

}